home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / arm-tdep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-03  |  10.9 KB  |  406 lines

  1. /* Target-dependent code for the Acorn Risc Machine, for GDB, the GNU Debugger.
  2.    Copyright 1988, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "frame.h"
  22. #include "inferior.h"
  23. #include "arm-opcode.h"
  24.  
  25. #include <sys/param.h>
  26. #include <sys/dir.h>
  27. #include <signal.h>
  28. #include <sys/ioctl.h>
  29. #include <sys/ptrace.h>
  30. #include <machine/reg.h>
  31.  
  32. #define N_TXTADDR(hdr) 0x8000
  33. #define N_DATADDR(hdr) (hdr.a_text + 0x8000)
  34.  
  35. #include "gdbcore.h"
  36. #include <sys/user.h>        /* After a.out.h  */
  37. #include <sys/file.h>
  38. #include <sys/stat.h>
  39.  
  40. #include <errno.h>
  41.  
  42.  
  43. /* Work with core dump and executable files, for GDB. 
  44.    This code would be in core.c if it weren't machine-dependent. */
  45.  
  46. /* Structure to describe the chain of shared libraries used
  47.    by the execfile.
  48.    e.g. prog shares Xt which shares X11 which shares c. */
  49.  
  50. struct shared_library {
  51.     struct exec_header header;
  52.     char name[SHLIBLEN];
  53.     CORE_ADDR text_start;    /* CORE_ADDR of 1st byte of text, this file */
  54.     long data_offset;        /* offset of data section in file */
  55.     int chan;            /* file descriptor for the file */
  56.     struct shared_library *shares; /* library this one shares */
  57. };
  58. static struct shared_library *shlib = 0;
  59.  
  60. /* Hook for `exec_file_command' command to call.  */
  61.  
  62. extern void (*exec_file_display_hook) ();
  63.    
  64. static CORE_ADDR unshared_text_start;
  65.  
  66. /* extended header from exec file (for shared library info) */
  67.  
  68. static struct exec_header exec_header;
  69.  
  70. void
  71. exec_file_command (filename, from_tty)
  72.      char *filename;
  73.      int from_tty;
  74. {
  75.   int val;
  76.  
  77.   /* Eliminate all traces of old exec file.
  78.      Mark text segment as empty.  */
  79.  
  80.   if (execfile)
  81.     free (execfile);
  82.   execfile = 0;
  83.   data_start = 0;
  84.   data_end -= exec_data_start;
  85.   text_start = 0;
  86.   unshared_text_start = 0;
  87.   text_end = 0;
  88.   exec_data_start = 0;
  89.   exec_data_end = 0;
  90.   if (execchan >= 0)
  91.     close (execchan);
  92.   execchan = -1;
  93.   if (shlib) {
  94.       close_shared_library(shlib);
  95.       shlib = 0;
  96.   }
  97.  
  98.   /* Now open and digest the file the user requested, if any.  */
  99.  
  100.   if (filename)
  101.     {
  102.       filename = tilde_expand (filename);
  103.       make_cleanup (free, filename);
  104.  
  105.       execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  106.             &execfile);
  107.       if (execchan < 0)
  108.     perror_with_name (filename);
  109.  
  110.       {
  111.     struct stat st_exec;
  112.  
  113. #ifdef HEADER_SEEK_FD
  114.     HEADER_SEEK_FD (execchan);
  115. #endif
  116.     
  117.     val = myread (execchan, &exec_header, sizeof exec_header);
  118.     exec_aouthdr = exec_header.a_exec;
  119.  
  120.     if (val < 0)
  121.       perror_with_name (filename);
  122.  
  123.     text_start = 0x8000;
  124.  
  125.     /* Look for shared library if needed */
  126.     if (exec_header.a_exec.a_magic & MF_USES_SL)
  127.         shlib = open_shared_library(exec_header.a_shlibname, text_start);
  128.  
  129.     text_offset = N_TXTOFF (exec_aouthdr);
  130.     exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text;
  131.  
  132.     if (shlib) {
  133.         unshared_text_start = shared_text_end(shlib) & ~0x7fff;
  134.         stack_start = shlib->header.a_exec.a_sldatabase;
  135.         stack_end = STACK_END_ADDR;
  136.     } else
  137.         unshared_text_start = 0x8000;
  138.     text_end = unshared_text_start + exec_aouthdr.a_text;
  139.  
  140.     exec_data_start = unshared_text_start + exec_aouthdr.a_text;
  141.         exec_data_end = exec_data_start + exec_aouthdr.a_data;
  142.  
  143.     data_start = exec_data_start;
  144.     data_end += exec_data_start;
  145.  
  146.     fstat (execchan, &st_exec);
  147.     exec_mtime = st_exec.st_mtime;
  148.       }
  149.  
  150.       validate_files ();
  151.     }
  152.   else if (from_tty)
  153.     printf ("No exec file now.\n");
  154.  
  155.   /* Tell display code (if any) about the changed file name.  */
  156.   if (exec_file_display_hook)
  157.     (*exec_file_display_hook) (filename);
  158. }
  159.  
  160. /* Read from the program's memory (except for inferior processes).
  161.    This function is misnamed, since it only reads, never writes; and
  162.    since it will use the core file and/or executable file as necessary.
  163.  
  164.    It should be extended to write as well as read, FIXME, for patching files.
  165.  
  166.    Return 0 if address could be read, EIO if addresss out of bounds.  */
  167.  
  168. int
  169. xfer_core_file (memaddr, myaddr, len)
  170.      CORE_ADDR memaddr;
  171.      char *myaddr;
  172.      int len;
  173. {
  174.   register int i;
  175.   register int val;
  176.   int xferchan;
  177.   char **xferfile;
  178.   int fileptr;
  179.   int returnval = 0;
  180.  
  181.   while (len > 0)
  182.     {
  183.       xferfile = 0;
  184.       xferchan = 0;
  185.  
  186.       /* Determine which file the next bunch of addresses reside in,
  187.      and where in the file.  Set the file's read/write pointer
  188.      to point at the proper place for the desired address
  189.      and set xferfile and xferchan for the correct file.
  190.  
  191.      If desired address is nonexistent, leave them zero.
  192.  
  193.      i is set to the number of bytes that can be handled
  194.      along with the next address.
  195.  
  196.      We put the most likely tests first for efficiency.  */
  197.  
  198.       /* Note that if there is no core file
  199.      data_start and data_end are equal.  */
  200.       if (memaddr >= data_start && memaddr < data_end)
  201.     {
  202.       i = min (len, data_end - memaddr);
  203.       fileptr = memaddr - data_start + data_offset;
  204.       xferfile = &corefile;
  205.       xferchan = corechan;
  206.     }
  207.       /* Note that if there is no core file
  208.      stack_start and stack_end define the shared library data.  */
  209.       else if (memaddr >= stack_start && memaddr < stack_end)
  210.     {
  211.         if (corechan < 0) {
  212.         struct shared_library *lib;
  213.         for (lib = shlib; lib; lib = lib->shares)
  214.             if (memaddr >= lib->header.a_exec.a_sldatabase &&
  215.             memaddr < lib->header.a_exec.a_sldatabase +
  216.               lib->header.a_exec.a_data)
  217.             break;
  218.         if (lib) {
  219.             i = min (len, lib->header.a_exec.a_sldatabase +
  220.                  lib->header.a_exec.a_data - memaddr);
  221.             fileptr = lib->data_offset + memaddr -
  222.             lib->header.a_exec.a_sldatabase;
  223.             xferfile = execfile;
  224.             xferchan = lib->chan;
  225.         }
  226.         } else {
  227.         i = min (len, stack_end - memaddr);
  228.         fileptr = memaddr - stack_start + stack_offset;
  229.         xferfile = &corefile;
  230.         xferchan = corechan;
  231.         }
  232.     }
  233.       else if (corechan < 0
  234.            && memaddr >= exec_data_start && memaddr < exec_data_end)
  235.     {
  236.       i = min (len, exec_data_end - memaddr);
  237.       fileptr = memaddr - exec_data_start + exec_data_offset;
  238.       xferfile = &execfile;
  239.       xferchan = execchan;
  240.     }
  241.       else if (memaddr >= text_start && memaddr < text_end)
  242.     {
  243.         struct shared_library *lib;
  244.         for (lib = shlib; lib; lib = lib->shares)
  245.         if (memaddr >= lib->text_start &&
  246.             memaddr < lib->text_start + lib->header.a_exec.a_text)
  247.             break;
  248.         if (lib) {
  249.         i = min (len, lib->header.a_exec.a_text +
  250.              lib->text_start - memaddr);
  251.         fileptr = memaddr - lib->text_start + text_offset;
  252.         xferfile = &execfile;
  253.         xferchan = lib->chan;
  254.         } else {
  255.         i = min (len, text_end - memaddr);
  256.         fileptr = memaddr - unshared_text_start + text_offset;
  257.         xferfile = &execfile;
  258.         xferchan = execchan;
  259.         }
  260.     }
  261.       else if (memaddr < text_start)
  262.     {
  263.       i = min (len, text_start - memaddr);
  264.     }
  265.       else if (memaddr >= text_end
  266.            && memaddr < (corechan >= 0? data_start : exec_data_start))
  267.     {
  268.       i = min (len, data_start - memaddr);
  269.     }
  270.       else if (corechan >= 0
  271.            && memaddr >= data_end && memaddr < stack_start)
  272.     {
  273.       i = min (len, stack_start - memaddr);
  274.     }
  275.       else if (corechan < 0 && memaddr >= exec_data_end)
  276.     {
  277.       i = min (len, - memaddr);
  278.     }
  279.       else if (memaddr >= stack_end && stack_end != 0)
  280.     {
  281.       i = min (len, - memaddr);
  282.     }
  283.       else
  284.     {
  285.       /* Address did not classify into one of the known ranges.
  286.          This shouldn't happen; we catch the endpoints.  */
  287.       fatal ("Internal: Bad case logic in xfer_core_file.");
  288.     }
  289.  
  290.       /* Now we know which file to use.
  291.      Set up its pointer and transfer the data.  */
  292.       if (xferfile)
  293.     {
  294.       if (*xferfile == 0)
  295.         if (xferfile == &execfile)
  296.           error ("No program file to examine.");
  297.         else
  298.           error ("No core dump file or running program to examine.");
  299.       val = lseek (xferchan, fileptr, 0);
  300.       if (val < 0)
  301.         perror_with_name (*xferfile);
  302.       val = myread (xferchan, myaddr, i);
  303.       if (val < 0)
  304.         perror_with_name (*xferfile);
  305.     }
  306.       /* If this address is for nonexistent memory,
  307.      read zeros if reading, or do nothing if writing.
  308.      Actually, we never right.  */
  309.       else
  310.     {
  311.       bzero (myaddr, i);
  312.       returnval = EIO;
  313.     }
  314.  
  315.       memaddr += i;
  316.       myaddr += i;
  317.       len -= i;
  318.     }
  319.   return returnval;
  320. }
  321.  
  322. /* APCS (ARM procedure call standard) defines the following prologue:
  323.  
  324.    mov        ip, sp
  325.   [stmfd    sp!, {a1,a2,a3,a4}]
  326.    stmfd    sp!, {...,fp,ip,lr,pc}
  327.   [stfe        f7, [sp, #-12]!]
  328.   [stfe        f6, [sp, #-12]!]
  329.   [stfe        f5, [sp, #-12]!]
  330.   [stfe        f4, [sp, #-12]!]
  331.    sub        fp, ip, #nn    // nn == 20 or 4 depending on second ins
  332. */
  333.  
  334. CORE_ADDR
  335. skip_prologue(pc)
  336. CORE_ADDR pc;
  337. {
  338.     union insn_fmt op;
  339.     CORE_ADDR skip_pc = pc;
  340.  
  341.     op.ins = read_memory_integer(skip_pc, 4);
  342.     /* look for the "mov ip,sp" */
  343.     if (op.generic.type != TYPE_ARITHMETIC ||
  344.     op.arith.opcode != OPCODE_MOV ||
  345.     op.arith.dest != SPTEMP ||
  346.     op.arith.operand2 != SP) return pc;
  347.     skip_pc += 4;
  348.     /* skip the "stmfd sp!,{a1,a2,a3,a4}" if its there */
  349.     op.ins = read_memory_integer(skip_pc, 4);
  350.     if (op.generic.type == TYPE_BLOCK_BRANCH &&
  351.     op.generic.subtype == SUBTYPE_BLOCK &&
  352.     op.block.mask == 0xf &&
  353.     op.block.base == SP &&
  354.     op.block.is_load == 0 &&
  355.     op.block.writeback == 1 &&
  356.     op.block.increment == 0 &&
  357.     op.block.before == 1) skip_pc += 4;
  358.     /* skip the "stmfd sp!,{...,fp,ip,lr,pc} */
  359.     op.ins = read_memory_integer(skip_pc, 4);
  360.     if (op.generic.type != TYPE_BLOCK_BRANCH ||
  361.     op.generic.subtype != SUBTYPE_BLOCK ||
  362.     /* the mask should look like 110110xxxxxx0000 */
  363.     (op.block.mask & 0xd800) != 0xd800 ||
  364.     op.block.base != SP ||
  365.     op.block.is_load != 0 ||
  366.     op.block.writeback != 1 ||
  367.     op.block.increment != 0 ||
  368.     op.block.before != 1) return pc;
  369.     skip_pc += 4;
  370.     /* check for "sub fp,ip,#nn" */
  371.     op.ins = read_memory_integer(skip_pc, 4);
  372.     if (op.generic.type != TYPE_ARITHMETIC ||
  373.     op.arith.opcode != OPCODE_SUB ||
  374.     op.arith.dest != FP ||
  375.     op.arith.operand1 != SPTEMP) return pc;
  376.     return skip_pc + 4;
  377. }
  378.  
  379. static void
  380. print_fpu_flags(flags)
  381. int flags;
  382. {
  383.     if (flags & (1 << 0)) fputs("IVO ", stdout);
  384.     if (flags & (1 << 1)) fputs("DVZ ", stdout);
  385.     if (flags & (1 << 2)) fputs("OFL ", stdout);
  386.     if (flags & (1 << 3)) fputs("UFL ", stdout);
  387.     if (flags & (1 << 4)) fputs("INX ", stdout);
  388.     putchar('\n');
  389. }
  390.  
  391. void
  392. arm_float_info()
  393. {
  394.     register unsigned long status = read_register(FPS_REGNUM);
  395.     int type;
  396.  
  397.     type = (status >> 24) & 127;
  398.     printf("%s FPU type %d\n",
  399.        (status & (1<<31)) ? "Hardware" : "Software",
  400.        type);
  401.     fputs("mask: ", stdout);
  402.     print_fpu_flags(status >> 16);
  403.     fputs("flags: ", stdout);
  404.     print_fpu_flags(status);
  405. }
  406.